/*
 * game.cc
 *
 *  Created on: 7 oct. 2011
 *      Author: eschmidmeier
 */

#include "../inc/game.hh"

void update_game()
    {

    }

void display_game(float interpolation)
    {
    interpolation = interpolation;
    }

// Main function using the game loop
// The Game loop use "Constant Game Speed independent of Variable FPS"
// to keep a smooth result on the device. It's mean that the game have
// constant update and the display is variable.

Game::Game()
    {

    }

Game::~GAME()
    {

    }

void Game::AddScreen(int screenId)
    {
    Frame* pFrame =
	    Osp::App::Application::GetInstance()->GetAppFrame()->GetFrame();

    switch (screenId)
	{
    case K_SCORES_SCREEN:
	{
	// Create the score card form
	_pScoresForm = new FormScores();
	_pScoresForm->Initialize();

	// Add the form to the frame
	pFrame->AddControl(*_pScoresForm);
	break;
	}
	// Similar cases for other forms
    default:
	break;
	}
    return;
    }

void Game::ShowScreen(int screenId)
{
   Frame* pFrame = Osp::App::Application::GetInstance()->GetAppFrame()->GetFrame();

   switch(screenId)
   {
      case K_SCORES_SCREEN:
      {
         // Set the current form
         pFrame->SetCurrentForm(*_pScoresForm);

         // Draw and show the form
         _pScoresForm->Draw();
         _pScoresForm->Show();
         break;
      }
      // Similar cases for other forms
      default:
         break;
   }
   return;
}

bool Game::OnAppInitializing(AppRegistry& appRegistry)
    {
    AddScreen( K_MENU_SCREEN);
    AddScreen( K_SCORES_SCREEN);
    AddScreen( K_GAMEPLAY_SCREEN);
    AddScreen( K_MULTIPLAYER_SCREEN);
    AddScreen( K_SETTINGS_SCREEN);
    ShowScreen(K_MENU_SCREEN);
    return true;
    }

int main()
    {
    Osp::Base::TimeSpan span(0, 0, 0);
    long next_tick = span.GetTicks();
    int loops = 0;
    float interpolation = 0;
    bool game_running = true;

    AppLog("Application started.");
    while (game_running)
	{
	loops = 0;

	while ((span.GetTicks() > next_tick) && (loops < MAX_FRAMESKIP))
	    {
	    update_game();

	    next_tick += SKIP_TICKS;
	    loops++;
	    }

	interpolation = float(span.GetTicks() + SKIP_TICKS - next_tick)
		/ float(SKIP_TICKS);
	display_game(interpolation);

	}
    return 0;
    }
